package com.shen.langchain4j.controller;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 低价API使用控制层接口
 */
@Slf4j
@RestController
@RequestMapping(value = "/lowApi")
public class LowApiController {
    @Autowired
    @Qualifier(value = "qwen")
    private ChatModel qwenModel;

    @Autowired
    @Qualifier(value = "deepseek")
    private ChatModel deepseekModel;

    /**
     * 常规提问大模型
     *
     * @param prompt 提示词
     * @return 大模型回答
     */
    @GetMapping(value = "/one")
    public String lowApiOne(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt) {
        return qwenModel.chat(prompt);
    }

    /**
     * 返回token消耗量
     *
     * @param prompt 提示词
     * @return token销毁
     */
    @GetMapping(value = "/two")
    public String lowApiTwo(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt) {
        ChatResponse chatResponse = qwenModel.chat(UserMessage.from(prompt));
        log.info("大模型输出结果为: {}", chatResponse.aiMessage().text());
        return chatResponse.metadata().tokenUsage().toString();
    }
}
